package com.jly.dao.impl;

import com.jly.dao.UserDAO;
import com.jly.io.Resources;
import com.jly.po.User;
import com.jly.sqlSession.SqlSession;
import com.jly.sqlSession.SqlSessionFactory;
import com.jly.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements UserDAO {

    @Override
    public List<User> findAll() throws Exception {
        InputStream resourceAsInputStream = Resources.getResourceAsInputStream("SqlMapConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsInputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        //调用
        List<User> userList = sqlSession.selectList("User.selectList");
        System.out.println("selectList结果遍历------");
        for (User u:userList) {
            System.out.println(u);
        }
        return userList;
    }

    @Override
    public User findByCondition(User user) throws Exception{
        InputStream resourceAsInputStream = Resources.getResourceAsInputStream("SqlMapConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsInputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user2 = sqlSession.selectOne("User.selectOne", user);
        System.out.println("selectOne查询结果------"+user2);
        return user2;
    }
}
